package com.baidu.shoppingmall.auth.visitor;


import com.baidu.shoppingmall.auth.dao.AuthorityDAO;
import com.baidu.shoppingmall.auth.domain.AuthorityPO;
import com.baidu.shoppingmall.auth.composite.AuthorityNode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 根据ID删除权限信息
 */
@SuppressWarnings("all")
@Repository
public class DeleteAuthorityVisitor implements AuthorityVisitor {
    @Autowired
    private AuthorityDAO authorityDAO;

    public DeleteAuthorityVisitor() {
    }

    @Override
    public void visit(AuthorityNode authorityNode) {
        //构造器，构造parent_id=parentId的条件查询条件
        QueryWrapper<AuthorityPO> queryWrapper=new QueryWrapper();
        Map<String, Object> map = new HashMap<>();
        map.put("parent_id", authorityNode.getId());
        queryWrapper.allEq(map);
        //根据权限的id查询子权限列表
        List<AuthorityPO> AuthorityPOs = authorityDAO.selectList(queryWrapper);
        List<AuthorityNode> permissionNodeList = new ArrayList<>(AuthorityPOs.size());
        AuthorityPOs.forEach(p -> {
            try {
                permissionNodeList.add(p.clone(AuthorityNode.class));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        if (permissionNodeList != null && permissionNodeList.size() > 0) {
            permissionNodeList.forEach(node->node.accept(this));
        }
        authorityDAO.deleteById(authorityNode.getId());
        System.out.println(authorityNode.getAuthorityComment()+"被删除");
    }
}
